<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/29
 * Time: 13:12
 */

namespace App\CronTask;


use App\Enums\SysKeyEnums;
use App\Enums\UrlEnums;
use EasySwoole\EasySwoole\Crontab\AbstractCronTask;
use EasySwoole\HttpClient\HttpClient;
use EasySwoole\MysqliPool\Mysql;

class SpecialCron extends AbstractCronTask
{
    //GET http://api.xxe.io/?resource=special&func=lists&event_id=65
    //4.1 返回某项目未开始和进行中的赛事专题列表
    public static function getRule(): string
    {
        // TODO: Implement getRule() method.
        return '@daily';
    }

    public static function getTaskName(): string
    {
        // TODO: Implement getTaskName() method.
        // 定时任务名称
        return 'taskTwo';
    }
    static function run(\swoole_server $server, int $taskId, int $fromWorkerId,$flags=null)
    {
        $db = Mysql::defer('mysql');
        $events = $db->get('sc_event');
        $specials = [];
        $url = UrlEnums::XXEURL;
        foreach ( $events as $event ){
            $option = [
                'timestamp' => time(),
                'resource'  => 'special',
                'func'      => 'lists',
                'event_id'  => $event['event_id']
            ];
            $queries = [
                'app_key'   => SysKeyEnums::APP_KEY,
                'app_secret'=> SysKeyEnums::APP_SECRET,
                'timestamp' => time(),
            ];
            $queries = array_merge($queries,$option);
            ksort($queries);
            $sign = '';
            foreach ( $queries as $k=>$v ){
                if( $v ){
                    $sign .= $k."={$v}&";
                }
            }
            if( $sign ){
                $sign = substr($sign, 0, -1);
            }
            $url = $url.'?'.$sign.'&sign='.strtolower(md5($sign));
            $client = new HttpClient($url);
            $response = $client->get();
            $content = $response->getBody();
            $content = json_decode($content, true);
            if( !isset($content['data']) || empty($content['data']) ){
                continue;
            }
            $data = $content['data'];
            foreach ( $data as $k=>$v ){
                if( $v['specials'] ){
                    $specials = array_merge($specials,$v['specials']);
                }
            }
        }
        foreach ( $specials as $k1=>$v1 ){
            $res = $db->where('special_id',(int)$v1['special_id'])
                ->getOne('sc_special');
            if( !$res ){
                $r = $db->insert('sc_special',[
                    'match_id'  => (int)$v1['match_id'],
                    'special_pic'=> $v1['special_pic'],
                    'start_time'=> $v1['start_time'],
                    'end_time'  => $v1['end_time'],
                    'special_name'=> $v1['special_name'],
                    'special_desc'=> $v1['special_desc'],
                    'bonus'     => $v1['bonus'],
                    'type'      => $v1['type'],
                    'team_count'=> (int)$v1['team_count'],
                    'special_id'=> (int)$v1['special_id']
                ]);
            }
        }
    }
}